# Copyright 2008 Fritz Thomas <fritztho@gmail.com> WebDevAppliance.com
# Distributed under the terms of the GNU General Public License v2
# $Header: $

DESCRIPTION="Base Files for the WebDevAppliance"
HOMEPAGE="http://webdevappliance.com"
SRC_URI="http://webdevappliance.com/files/${PF}.tar.bz2"

LICENSE="GPLv3"
SLOT="0"
KEYWORDS="x86"
IUSE=""

NEW_INSTALL=true
BASE_CONFIG_FILE="/etc/webdevappliance/base.conf"

DEPEND=">=dev-db/mysql-5
	>=www-servers/apache-2.2
	>=dev-lang/php-5.2
	>=sys-auth/pam_mysql-0.7_rc1-r1
	>=sys-auth/libnss-mysql-1.5
	>=app-admin/sudo-1.6.9_p16
	>=app-misc/screen-4.0.3"
RDEPEND="${DEPEND}"

RESTRICT="mirror"

inherit eutils pam

pkg_setup() {
	if [ -f $BASE_CONFIG_FILE ] ; then
		NEW_INSTALL=false
	else
		echo
		eerror "Make sure you have set the following USE Flags and ENV Variables!!!"
		eerror "Otherwise all other packages depending on ${PN} Package might not work!"
		echo
		echo
		eerror "Add this variables to /etc/make.conf:"
		eerror "APACHE2_MPMS=\"prefork\""
		eerror "SUEXEC_DOCROOT=\"/home\""
		eerror "SUEXEC_MINUID=\"10000\""
		eerror "SUEXEC_MINGID=\"10000\""
		eerror "PORTAGE_ECLASS_WARNING_ENABLE=\"0\""
		echo
		echo
		eerror "Add AT LEAST these USE Flags to /etc/portage/package.use:"
		eerror "www-servers/apache -threads suexec"
		eerror "sys-auth/pam_mysql openssl"
		eerror "dev-lang/php -apache2 force-cgi-redirect cgi mysqli mysql pear"
		eerror "reflection session pdo cli pcre hash mhash json suhosin curl"
		ebeep 8
	fi
}


src_install() {
	insinto /etc/
	doins files/etc/*
	fperms 0600 "/etc/libnss-mysql-root.cfg"
	fperms 0600 "/etc/libnss-mysql.cfg"
	fperms 0440 "/etc/sudoers"

	insinto /etc/bash/
	doins files/etc/bash/*

	insinto /etc/apache2/
	doins files/etc/apache2/*

	dodir /etc/apache2/includes
	insinto /etc/apache2/includes
	doins files/etc/apache2/includes/*

	dodir /etc/skel/vhosts

	dopamd files/etc/pam.d/*
	fperms 0600 "/etc/pam.d/system-auth"

	insinto /etc/ssh/
	doins files/etc/ssh/*
	fperms 0600 "/etc/ssh/sshd_config"

	dodir /etc/webdevappliance
	insinto /etc/webdevappliance
	doins files/etc/webdevappliance/*
	fperms 0600 "/etc/webdevappliance/base.conf"

	dodir /usr/lib/webdevappliance
	insinto /usr/lib/webdevappliance
	doins files/usr/lib/webdevappliance/*

	dodir /usr/lib/webdevappliance/setup
	insinto /usr/lib/webdevappliance/setup
	doins files/usr/lib/webdevappliance/setup/*
}


src_test() {
	true
}

src_compile() {
	true
}

pkg_preinst() {
	if [ true == $NEW_INSTALL ] ; then
		source ${D}/etc/webdevappliance/base.conf
	else
		source ${ROOT}/etc/webdevappliance/base.conf
	fi
	HOSTNAME=`hostname -s`
	sed -i "s/#DATABASE#/${WDA_DATABASE}/g
			s/#USERNAME#/${LIBNSS_USER}/g
			s/#PASSWORD#/${LIBNSS_PASSWORD}/g
			s/#ROOTUSERNAME#/${LIBNSS_ROOT_USER}/g
			s/#ROOTPASSWORD#/${LIBNSS_ROOT_PASSWORD}/g
			s/#HOSTNAME#/${HOSTNAME}/g" \
			${D}/etc/libnss-mysql-root.cfg ${D}/etc/libnss-mysql.cfg \
			${D}/etc/issue ${D}/etc/nsswitch.conf ${D}/etc/pam.d/samba ${D}/etc/pam.d/system-auth
}

pkg_postinst() {
	einfo "Run emerge --config \"=$CATEGORY/$PF\" to configure this package."
}


pkg_config() {
	source ${ROOT}/etc/webdevappliance/base.conf
	HOSTNAME=`hostname -s`
	sed "s/#DATABASE#/${WDA_DATABASE}/g
	s/#USERNAME#/${LIBNSS_USER}/g
    s/#PASSWORD#/${LIBNSS_PASSWORD}/g
    s/#ROOTUSERNAME#/${LIBNSS_ROOT_USER}/g
    s/#ROOTPASSWORD#/${LIBNSS_ROOT_PASSWORD}/g
    s/#HOSTNAME#/${HOSTNAME}/g" \
	"${ROOT}/usr/lib/webdevappliance/setup/mysql.sql" > "$T/mysql.sql"

	sed "s/#DATABASE#/${WDA_DATABASE}/g
    s/#USERNAME#/${LIBNSS_USER}/g
    s/#PASSWORD#/${LIBNSS_PASSWORD}/g
    s/#ROOTUSERNAME#/${LIBNSS_ROOT_USER}/g
    s/#ROOTPASSWORD#/${LIBNSS_ROOT_PASSWORD}/g
    s/#HOSTNAME#/${HOSTNAME}/g" \
	"${ROOT}/usr/lib/webdevappliance/setup/webdevappliance.sql" > "$T/webdevappliance.sql"

	einfo "Executing: mysql -uroot -p mysql < ${T}/mysql.sql"
	mysql -uroot -p mysql < "${T}/mysql.sql"
	
	einfo "Executing: mysql-uroot -p $DATABASE < ${T}/webdevappliance.sql"
	mysql -uroot -p $DATABASE < "${T}/webdevappliance.sql"
	rm "$T/webdevappliance.sql" "$T/mysql.sql"
}
